package com.zzh.partnersys.im.handler;

import cn.hutool.core.net.url.UrlBuilder;
import com.zzh.partnersys.im.session.SessionFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

//获取用户请求时携带的参数，同时直接进行绑�?
public class HttpGetUserIdHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof FullHttpRequest) {
            FullHttpRequest request = (FullHttpRequest) msg;
            UrlBuilder urlBuilder = UrlBuilder.ofHttp(request.uri());

            // 获取token参数
            String userId = Optional.ofNullable(urlBuilder.getQuery()).map(k->k.get("userId")).map(CharSequence::toString).orElse("");
            System.out.println("userId"+userId);
            //绑定用户
            if (!StringUtils.isBlank(userId)){
                System.out.println("绑定用户"+userId);
                SessionFactory.getSession().bind(ctx.channel(),Long.parseLong(userId));
            }

            ctx.fireChannelRead(request);
        }else {
            ctx.fireChannelRead(msg);
        }
    }


}